Partial Class Case_caseEmployee
    Inherits System.Web.UI.UserControl
    Implements IPermission

    Const C_EDITCOLUMN As Integer = 4
    Const C_VIEW_COLUMN As Integer = 5

    Public Property ParticipantObject() As Cdm.DataCore.Entities.Participant
        Get
            Dim permissionPage As IPermission = Me.Page
            Return permissionPage.PrimaryObject
        End Get
        Set(ByVal value As Cdm.DataCore.Entities.Participant)
            BindDropdowns()
            BindGrid()
            SetPermissionView()
        End Set
    End Property

    Private Property EditIndex() As Integer
        Get
            If ViewState("editIndex") Is Nothing Then
                Return -1
            Else
                Return Convert.ToInt32(ViewState("editIndex"))
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("editIndex") = value
        End Set
    End Property

    Private ReadOnly Property AddVisible() As Boolean
        Get
            Return UserAuthorization.HasInsertPermission(Me.EntityPermissionType)
        End Get
    End Property

    Private Sub SetPermissionView()

        Me.AddEmployeeButton.Visible = Me.AddVisible
        If UserAuthorization.HasUpdatePermission(Me.EntityPermissionType) Then
            Me.EmployeeGridView.Columns(C_EDITCOLUMN).Visible = True
            Me.EmployeeGridView.Columns(C_VIEW_COLUMN).Visible = False
        Else
            Me.EmployeeGridView.Columns(C_EDITCOLUMN).Visible = False
            Me.EmployeeGridView.Columns(C_VIEW_COLUMN).Visible = True
        End If

    End Sub


    Private Sub BindGrid()

        'Dim employeeList As System.Collections.Generic.List(Of Cdm.DataCore.Entities.ParticipantEmployee)
        Dim participantId As Integer = EntityController.GetParticipant.ParticipantId

        Me.EmployeeGridView.DataSource = ParticipantObject.GetEmployees(participantId)
        Me.EmployeeGridView.DataBind()
        Me.EmployeeGridView.Visible = True

    End Sub

    Protected Sub AddEmployeeButton_Click(ByVal sender As Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles AddEmployeeButton.Click

        ShowPanel()
        ClearValues()

    End Sub

    Private Sub ClearValues()

        Dim i As Integer
        Dim inpt As InputControls.IInputControl
        For i = 0 To Me.DetailPanel.Controls.Count - 1
            If TypeOf (DetailPanel.Controls(i)) Is InputControls.IInputControl Then
                inpt = DetailPanel.Controls(i)
                inpt.Value = ""
            End If
        Next

    End Sub


#Region "DropDown DataSources"
    Public Function EmployeeTypes() As System.Collections.Generic.List(Of Cdm.DataCore.LookupTables.LuEmployeeType)
        Return Cdm.DataCore.LookupTables.LuEmployeeType.ListAll()
    End Function

    Public Function ServiceTypes() As System.Collections.Generic.List(Of Cdm.DataCore.LookupTables.LuEmployeeServiceType)
        Return Cdm.DataCore.LookupTables.LuEmployeeServiceType.ListAll()
    End Function
#End Region

    Protected Sub EmployeeGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles EmployeeGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(EmployeeGridView)
    End Sub

    Protected Sub EmployeeGridView_RowEditing(ByVal sender As Object, _
                                              ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) _
                                              Handles EmployeeGridView.RowEditing

        Me.EditIndex = e.NewEditIndex
        Dim isEditable As Boolean = UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        EntityController.SetPageValuesFromObject(Me.DetailPanel.Controls, Me.ParticipantObject.Employees(e.NewEditIndex), isEditable)
        Me.EmployeeSpecialFieldControl.Editable = isEditable
        Me.EmployeeSpecialFieldControl.IsBound = False
        Me.EmployeeSpecialFieldControl.PrimaryObject = Me.ParticipantObject.Employees(e.NewEditIndex)
        e.Cancel = True
        ShowPanel()

    End Sub

    Private Sub ShowPanel()

        Me.AddEmployeeButton.Visible = False
        Me.EmployeeGridView.Visible = False
        Me.DetailPanel.Visible = True
        Dim savePage As IPageSave = Me.Page
        savePage.SaveVisible = False

    End Sub

    Private Sub ShowGrid()

        Me.AddEmployeeButton.Visible = Me.AddVisible
        BindGrid()
        Me.DetailPanel.Visible = False
        Dim savePage As IPageSave = Me.Page
        savePage.SaveVisible = True

    End Sub

    Public ReadOnly Property EntityPermissionType() As Cdm.DataCore.UserSecurity.Permission.PermissionType Implements IPermission.EntityPermissionType
        Get
            Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseEmployee
        End Get
    End Property

    Public Sub ResetControls() Implements IPermission.ResetControls
        'TODO If the Formview is showing, hide it.
    End Sub

    Public Property PrimaryObject() As Cdm.DataCore.ConfigurableObject Implements IPermission.PrimaryObject
        Get
            Return Nothing
        End Get
        Set(ByVal value As Cdm.DataCore.ConfigurableObject)
            'nothing to do
        End Set
    End Property

    Public ReadOnly Property HasEditPermission() As Boolean
        Get
            Return UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        End Get
    End Property

    Public ReadOnly Property SectionName() As String Implements IPermission.SectionName
        Get
            Return "Employees"
        End Get
    End Property

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        Dim isEditable As Boolean = UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        If Not Me.IsPostBack And Not isEditable Then
            Me.CancelButton.Text = "Return to Grid"
            Me.SaveButton.Visible = False
        End If

        If Not Me.IsPostBack Then
            BindDropdowns()
        End If

        Dim emp As New Cdm.DataCore.Entities.ParticipantEmployee()
        Me.EmployeeSpecialFieldControl.Editable = isEditable
        Me.EmployeeSpecialFieldControl.PrimaryObject = emp

        EntityController.SetPagePropertiesFromObject(Me.DetailPanel.Controls, emp, isEditable, False, emp.EntityTableName)

        SetPermissionView()
        Me.VerifyUpdateLabel.Visible = False
        Me.EmployeeValidateError.Visible = False

    End Sub

    Private Sub BindDropdowns()

        Me.EditServiceTypeInputControl.DataSource = Cdm.DataCore.LookupTables.LuEmployeeServiceType.ListAll(0, "Select Service Type")
        Me.EditServiceTypeInputControl.DataTextField = "Description"
        Me.EditServiceTypeInputControl.DataValueField = "Id"
        Me.EditServiceTypeInputControl.DataBind()
        Me.EditTypeInputControl.DataSource = Cdm.DataCore.LookupTables.LuEmployeeType.ListAll(0, "Select Employee Type")
        Me.EditTypeInputControl.DataTextField = "Description"
        Me.EditTypeInputControl.DataValueField = "Id"
        Me.EditTypeInputControl.DataBind()

    End Sub

    Public ReadOnly Property ControlCollection() As System.Web.UI.ControlCollection Implements IPermission.ControlCollection
        Get
            Return Me.DetailPanel.Controls
        End Get
    End Property

    Protected Sub saveEmployee_Click(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles SaveButton.Click

        Dim editEmployee As Cdm.DataCore.Entities.ParticipantEmployee
        If Me.EditIndex = -1 Then
            editEmployee = New Cdm.DataCore.Entities.ParticipantEmployee
            Me.VerifyUpdateLabel.Text = "Employee added.  Click 'Save File' to permanently add this Employee to the file."
        Else
            editEmployee = Me.ParticipantObject.Employees(Me.EditIndex)
            Me.VerifyUpdateLabel.Text = "Employee updated.  Click 'Save File' to permanently save these changes to the file."
        End If

        editEmployee.Reset()
        EntityController.SetObjectPropertiesFromPage(Me.DetailPanel.Controls, editEmployee, Me.SectionName)
        editEmployee.SpecialFieldsValues = Me.EmployeeSpecialFieldControl.SaveSpecialFields()

        Dim alError As System.Collections.ArrayList = editEmployee.Validate()

        If editEmployee.ValidateErrors.Count > 0 Then
            Me.EmployeeValidateError.SetErrors(alError)
            Me.EmployeeValidateError.Message = "Can not update employee."
            Me.EmployeeValidateError.Visible = True
        Else
            If Me.EditIndex = -1 Then
                Me.ParticipantObject.Employees.Add(editEmployee)
            End If
            Me.EditIndex = -1
            ShowGrid()
            Me.VerifyUpdateLabel.Visible = True
        End If

    End Sub

    Protected Sub cancelSave_Click(ByVal sender As Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles CancelButton.Click

        Me.EditIndex = -1
        ShowGrid()

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        Me.VerifyUpdateLabel.Visible = False
        Me.EmployeeValidateError.Visible = False
        Me.EmployeeScheduleHyperLink.NavigateUrl = "EmployeeScheduler.aspx?id=" + EntityController.GetParticipant().ParticipantId.ToString()
    End Sub

End Class
